package msg

import (
	"context"
	"git.mycaigou.com/gfyx/common/tenant_db"
	"git.mycaigou.com/gfyx/common/usermetadata"
	"git.mycaigou.com/gfyx/micro-gfyx-oppty-service/infrastructure/common/constants"
	"git.mycaigou.com/gfyx/micro-gfyx-oppty-service/infrastructure/common/utils"
	basePb "git.mycaigou.com/gfyx/micro-gfyx-oppty-service/proto/micro_gfyx_oppty_service_proto/micro-gfyx-oppty-service/base"
	"time"
)

type msgRepository struct {
	tenant_db.TenantDB
}

// NewMsgRepository 构造函数
func NewMsgRepository() MsgRepositoryIface {
	return &msgRepository{}
}

func (r *msgRepository) SetReaded(ctx context.Context, req *basePb.MsgSetReadedRequest) (int64, error) {
	metaUserInfo := usermetadata.GetMetaUserInfo(ctx)
	receiverId := metaUserInfo.UserId
	err := r.GetTenantDB(ctx).Table(GfyxMessageInboxPo{}.TableName()).Where("is_deleted = ? and receiver_id = ? and id = ?", constants.IsDeletedFalse, receiverId, req.Id).
		Updates(map[string]interface{}{
			"is_readed":   1,
			"readed_time": time.Now(),
		}).Error
	return req.Id, err
}

func (r *msgRepository) GetReadStatusGroupSummary(ctx context.Context, isReaded int64) (int64, error) {
	metaUserInfo := usermetadata.GetMetaUserInfo(ctx)
	receiverId := metaUserInfo.UserId
	db := r.GetTenantDB(ctx).Table(GfyxMessageInboxPo{}.TableName()).Where("is_deleted = ? and receiver_id = ?", constants.IsDeletedFalse, receiverId)
	db = db.Where("is_readed = ?", isReaded)
	err := db.Count(&isReaded).Error
	return isReaded, err
}

// List 消息列表
func (r *msgRepository) List(ctx context.Context, req *basePb.MsgListRequest) ([]*GfyxMessageInboxPo, *utils.PageInfo, error) {
	msgList := make([]*GfyxMessageInboxPo, 0)
	metaUserInfo := usermetadata.GetMetaUserInfo(ctx)
	receiverId := metaUserInfo.UserId
	db := r.GetTenantDB(ctx).Table(GfyxMessageInboxPo{}.TableName()).Where("is_deleted = ? and receiver_id = ?", constants.IsDeletedFalse, receiverId)
	// 所有消息
	if req.IsReaded == 0 {
	} else {
		// 已读或未读
		db = db.Where("is_readed = ?", req.IsReaded)
	}

	var total int64
	err := db.Count(&total).Error
	if err != nil {
		return nil, nil, err
	}

	page := 1
	pageSize := 20
	if req.Page != nil {
		page = int(req.Page.Page)
		pageSize = int(req.Page.PageSize)
		if page <= 0 {
			page = 1
		}
		if pageSize <= 0 {
			pageSize = 20
		}
		if pageSize >= 100 {
			pageSize = 100
		}
	}
	db = db.Order("created_on desc").Offset((page - 1) * pageSize).Limit(pageSize)
	err = db.Find(&msgList).Error
	pageInfo := &utils.PageInfo{
		Page:     page,
		PageSize: pageSize,
		Total:    total,
	}

	return msgList, pageInfo, err
}

func (r *msgRepository) PushSiteMsg(ctx context.Context, data *GfyxMessageInboxPo) error {
	data.CreatedOn = time.Now()
	data.ModifiedOn = time.Now()

	if err := r.GetTenantDB(ctx).Create(data).Error; err != nil {
		return err
	}

	return nil
}

func (r *msgRepository) BatchPushSiteMsg(ctx context.Context, messageList []*GfyxMessageInboxPo) error {
	if len(messageList) == 0 {
		return nil
	}
	if err := r.GetTenantDB(ctx).Create(messageList).Error; err != nil {
		return err
	}

	return nil
}
